package com.example.wechat.controller;





import com.alibaba.fastjson.JSONObject;
import com.example.educate.model.wechat.Menu;
import com.example.educate.result.Result;
import com.example.educate.vo.wechat.MenuVo;
import com.example.wechat.service.MenuService;
import com.example.wechat.utils.ConstantPropertiesUtil;
import com.example.wechat.utils.HttpClientUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 订单明细 订单明细 前端控制器
 * </p>
 *
 * @author Xiao
 * @since 2022-09-18
 */
@Api(tags = "微信服务号接口")
@RestController
@RequestMapping("/admin/wechat/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

//    removeMenu
    @DeleteMapping("removeMenu")
    @ApiOperation("公众号菜单的删除")
    public Result removeMenu(){
        menuService.removeMenu();
        return Result.ok(null);
    }

    @GetMapping("syncMenu")
    @ApiOperation("同步微信菜单的接口")
    public Result syncMenu(){
        menuService.syncMenu();
        return Result.ok(null);
    }


    @GetMapping("getAccessToken")
    @ApiOperation("获取微信中的access_token接口，微信同步接口")
    public Result getAccessToken(){
        try {
            StringBuffer Getbuffer = new StringBuffer();
            Getbuffer.append("https://api.weixin.qq.com/cgi-bin/token");
            Getbuffer.append("?grant_type=client_credential");
            Getbuffer.append("&appid=%s");
            Getbuffer.append("&secret=%s");
            String url = String.format(Getbuffer.toString(),
                     ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET);

            String tokenString = HttpClientUtils.get(url);

            JSONObject json = JSONObject.parseObject(tokenString);
            String access_token = json.getString("access_token");
            return Result.ok(access_token);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(null);
        }
    }


    @GetMapping("findMenuInfo")
    @ApiOperation("获取所有的菜单，按照一级和二级菜单进行封装")
    public Result findMenuInfo(){
        List<MenuVo> menuVoList = menuService.findMenuInfo();
        return Result.ok(menuVoList);
    }


    @GetMapping("findOneMenuInfo")
    @ApiOperation("获取所有的一级菜单")
    public Result findMenuOneInfo(){
        List<Menu> menuList = menuService.findOneMenuInfo();
        return Result.ok(menuList);
    }

    @ApiOperation(value = "根据id进行查询")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id){
        Menu menu = menuService.getById(id);
        return Result.ok(menu);
    }


    @ApiOperation(value = "新增菜单接口")
    @PostMapping("save")
    public Result save(@RequestBody Menu menu){
        menuService.save(menu);
        return Result.ok(null);
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("update")
    public Result updateById(@RequestBody Menu menu){
        menuService.updateById(menu);
        return Result.ok(null);
    }

    @ApiOperation(value = "根据id删除菜单")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id){
        menuService.removeById(id);
        return Result.ok(null);
    }


    @ApiOperation(value = "根据id列表删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        menuService.removeByIds(idList);
        return Result.ok(null);
    }
}

